﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using WuDada.Core.Auth;
using WuDada.Core.Auth.Service;
using WuDada.Core.Auth.Domain;
using WuDada.Core.Auth.Container;
using WuDada.Core.Post.Domain;
using WuDada.Core.Post;
using WuDada.Core.Post.Service;
using WuDada.Core.SystemApplications.Domain;
using WuDada.Core.SystemApplications;
using WuDada.Core.SystemApplications.Service;
using System.Globalization;
using WuDada.Core.Generic.Mail;
using WuDada.Core.Web.Generic.Helper;
using System.Data;
using System.IO;

namespace RichFree.Test.Service
{
    [TestFixture]
    public class InitWebDataService
    {
        private AuthFactory m_AuthFactory { get; set; }
        private PostFactory m_PostFactory { get; set; }
        private SystemFactory m_SystemFactory { get; set; }
        private IAuthService m_AuthService { get; set; }
        private IPostService m_PostService { get; set; }
        private ITemplateService m_TemplateService { get; set; }
        private ISystemService m_SystemService { get; set; }
        private IMessageService m_MessageService { get; set; }
        private ICityService m_CityService { get; set; }

        [TestFixtureSetUp]
        public void TestCaseInit()
        {
            m_AuthFactory = new AuthFactory();
            m_PostFactory = new PostFactory();
            m_SystemFactory = new SystemFactory();
            m_AuthService = m_AuthFactory.GetAuthService();
            m_PostService = m_PostFactory.GetPostService();
            m_TemplateService = m_SystemFactory.GetTemplateService();
            m_SystemService = m_SystemFactory.GetSystemService();
            m_MessageService = m_PostFactory.GetMessageService();
            m_CityService = m_SystemFactory.GetCityService();
        }

        [Test]
        public void InitWebData()
        {
            InitMenu();
            InitLoginRoleAndUser();
            InitNode();
            InitSystemParam();
            InitCity();
        }

        private void InitCity()
        {
            FileInfo fileInfo = new FileInfo("File/Zip32_10108.xls");
            FileStream fs = fileInfo.OpenRead();
            string sheetName = "Sheet1";
            DataTable dtData = NPOIHelper.RenderDataTableFromExcel(fs, sheetName, 0);

            for (int row = 0; row < dtData.Rows.Count; row++)
            {
                string zipode = dtData.Rows[row][0].ToString();
                string cityName = dtData.Rows[row][1].ToString();
                string areaName = dtData.Rows[row][2].ToString();

                CityVO cityVO = m_CityService.GetCityByCityName(cityName);
                if (cityVO == null)
                {
                    cityVO = new CityVO(cityName);
                    cityVO = m_CityService.CreateCity(cityVO);
                }

                AreaVO areaVO = m_CityService.GetAreaByAreaNameAndCityId(areaName, cityVO.CityId);
                if (areaVO == null)
                {
                    areaVO = new AreaVO(cityVO, areaName, zipode.Substring(0, 3));
                    areaVO = m_CityService.CreateArea(areaVO);
                }
            }
        }

        [Test]
        public void Test_UserMenuFuncContainer()
        {
            //清除快取
            UserMenuFuncContainer.GetInstance().ReloadAllMenu();

             UserMenuFuncContainer.GetInstance().GetUser("admin");
            UserMenuFuncContainer.GetInstance().GetUser("admin");
        }

        [Test]
        public void Test_InitContactorMail()
        {
            string classify = "聯絡我們收件者";
            ItemParamVO contactor = new ItemParamVO(classify, "吳信達", "dada2cindy@hotmail.com", false);
            m_SystemService.CreateItemParam(contactor);

            ItemParamVO contactor2 = new ItemParamVO(classify, "王小小", "dada@pro2e.com.tw", false);
            m_SystemService.CreateItemParam(contactor2);

            IList<ItemParamVO> contactorList = m_SystemService.GetAllItemParamByNoDel(classify);
            Console.WriteLine("contactorList count =" + contactorList.Count);
        }

        [Test]
        public void Test_SendMessageMail()
        {
            //建立一篇訊息
            MessageVO messageVO = new MessageVO();
            messageVO.Content = "意見";
            messageVO.CreateName = "張大保";
            messageVO.EMail = "dada2338@yahoo.com.tw";
            messageVO.Fax = "23223333";
            messageVO.Phone = "22234563";
            messageVO.Mobile = "0912333444";
            messageVO.CreatedDate = DateTime.Now;
            messageVO.CreateIP = "127.0.0.1";

            messageVO = m_MessageService.CreateMessage(messageVO);

            string classify = "聯絡我們收件者";
            IList<ItemParamVO> contactorList = m_SystemService.GetAllItemParamByNoDel(classify);

            if (contactorList != null && contactorList.Count > 0)
            {
                SystemParamVO mailVO = m_SystemService.GetSystemParamByRoot();
                MailService mailService = new MailService(mailVO.MailSmtp, int.Parse(mailVO.MailPort), mailVO.EnableSSL, mailVO.Account, mailVO.Password);

                StringBuilder sbMailList = new StringBuilder();
                foreach (ItemParamVO contactor in contactorList)
                {
                    sbMailList.Append(string.Format("{0};", contactor.Value));
                }

                string mailTitle = string.Format("收到一封由【{0}】從富裕自由國際管顧問網站提出的意見信。", messageVO.CreateName);
                string mailContent = GenMailContent(messageVO);

                mailService.SendMail(mailVO.SendEmail, sbMailList.ToString(), mailTitle, mailContent);
            }
        }

        private string GenMailContent(MessageVO messageVO)
        {
            StringBuilder sbContent = new StringBuilder();

            sbContent.Append(string.Format("時　　間：{0}<br />", messageVO.CreatedDate.Value.ToString()));
            sbContent.Append(string.Format("姓　　名：{0}<br />", messageVO.CreateName));
            sbContent.Append(string.Format("電　　話：{0}<br />", messageVO.Phone));
            sbContent.Append(string.Format("手　　機：{0}<br />", messageVO.Mobile));
            sbContent.Append(string.Format("傳　　真：{0}<br />", messageVO.Fax));
            sbContent.Append(string.Format("電子信箱：{0}<br />", messageVO.EMail));
            sbContent.Append(string.Format("意　　見：{0}<br />", messageVO.Content.Replace("\n", "<br />")));

            return sbContent.ToString();
        }

        [Test]
        public void TestDay()
        {
            TaiwanLunisolarCalendar tlc = new TaiwanLunisolarCalendar();

            int year = tlc.GetYear(DateTime.Now);
            int month = tlc.GetMonth(DateTime.Now);
            int day = tlc.GetDayOfMonth(DateTime.Now);

            string aa = string.Format("{0}/{1}/{2}", year, month, day);
            Console.WriteLine("day = " + aa);

            DateTime date2 = tlc.ToDateTime(year, month, day, 0, 0, 0, 0);
            Console.WriteLine("day2 = " + date2.ToShortDateString());
        }

        [Test]
        public void TestCurrentTemplate()
        {
            TemplateVO templateVO = m_TemplateService.GetCurrentTemplate(7);
            Console.WriteLine("TestCurrentTemplate = " + templateVO.Name);
        }

        [Test]
        public void TestGetPostListByNodeId()
        {
            int nodeId = 22;

            //搜尋條件
            DateTime? startDate = DateTime.Today;
            string sortField = "ShowDate";
            bool sortDesc = true;
            int pageSize = 2;

            IList<PostVO> postList = m_PostService.GetPostListByNodeId(nodeId, true, startDate, 0, pageSize, sortField, sortDesc);

            int count = postList.Count;
            Console.WriteLine("postList count = " + count);
        }

        private void InitMenu()
        {
            MenuFuncVO parentMenu1 = CreateParentMenu("網站分類管理", 1000);
            CreateSubMenu("講座活動-近期活動分類編輯", parentMenu1, 1001, "admin/UC01/0101.aspx");

            MenuFuncVO parentMenu4 = CreateParentMenu("網站內容管理", 2000);
            CreateSubMenu("最新消息", parentMenu4, 2001, "admin/UC02/0201.aspx");
            CreateSubMenu("公司介紹", parentMenu4, 2002, "admin/UC02/0202.aspx");
            CreateSubMenu("師資陣容", parentMenu4, 2003, "admin/UC02/0203.aspx");
            MenuFuncVO subMenuFuncVO4_4 = CreateSubMenu("講座活動-近期活動", parentMenu4, 2004, "admin/UC02/0204.aspx");
            m_AuthService.AddOtherPath(subMenuFuncVO4_4, "admin/UC02/0204_1.aspx");//報名清單

            CreateSubMenu("講座活動-講座介紹", parentMenu4, 2005, "admin/UC02/0205.aspx");
            MenuFuncVO subMenuFuncVO4_6 = CreateSubMenu("精彩回顧", parentMenu4, 2006, "admin/UC02/0206.aspx");
            m_AuthService.AddOtherPath(subMenuFuncVO4_6, "admin/UC02/0206_1.aspx");//影片
            m_AuthService.AddOtherPath(subMenuFuncVO4_6, "admin/UC02/0206_2.aspx");//照片

            CreateSubMenu("成功見證", parentMenu4, 2007, "admin/UC02/0207.aspx");
            CreateSubMenu("財商文章", parentMenu4, 2008, "admin/UC02/0208.aspx");
            CreateSubMenu("行事曆", parentMenu4, 2009, "admin/UC02/0209.aspx");
            CreateSubMenu("聯絡我們", parentMenu4, 2009, "admin/UC02/0210.aspx");

            MenuFuncVO parentMenu5 = CreateParentMenu("線上諮詢管理", 5000);
            CreateSubMenu("線上諮詢紀錄", parentMenu5, 5001, "admin/UC05/0501.aspx");
            CreateSubMenu("信箱設定", parentMenu5, 5002, "admin/UC05/0502.aspx");

            MenuFuncVO parentMenu6 = CreateParentMenu("會員專區管理", 6000);
            CreateSubMenu("會員管理", parentMenu6, 6001, "admin/UC06/0601.aspx");

            MenuFuncVO parentMenu2 = CreateParentMenu("個人設定", 9998);
            CreateSubMenu("登入密碼變更", parentMenu2, 1, "admin/UC30/3001Personal.aspx");

            MenuFuncVO parentMenu3 = CreateParentMenu("權限管理", 9999);
            CreateSubMenu("帳號管理", parentMenu3, 1, "admin/UC14/UserAdd.aspx");
            CreateSubMenu("群組管理", parentMenu3, 2, "admin/UC14/RoleAdd.aspx");
            CreateSubMenu("帳號群組設定", parentMenu3, 3, "admin/UC14/UserRoleSet.aspx");
            CreateSubMenu("群組權限設定", parentMenu3, 4, "admin/UC14/RoleFuncSet.aspx");
            CreateSubMenu("使用紀錄", parentMenu3, 5, "admin/UC14/QueryLog.aspx");
        }

        private void InitLoginRoleAndUser()
        {
            //建立後台角色
            LoginRoleVO loginRoleVO = new LoginRoleVO("系統管理員");
            loginRoleVO.MenuFuncList = m_AuthService.GetNotTopMenuFunc(); //角色功能權限
            m_AuthService.CreateLoginRole(loginRoleVO);

            LoginUserVO loginUserVO = new LoginUserVO();
            loginUserVO.UserId = "admin";
            loginUserVO.Password = "1234";
            loginUserVO.FullNameInChinese = "系統管理者";
            loginUserVO.FullNameInEnglish = "Administrator";
            loginUserVO.LoginRoleList = new List<LoginRoleVO>();
            loginUserVO.LoginRoleList.Add(loginRoleVO);
            loginUserVO.CreateDate = DateTime.Now;
            m_AuthService.CreateLoginUser(loginUserVO);
        }

        private void InitNode()
        {
            NodeVO rootNodeVO = CreateNode("Root", null, 0);
            CreateNode("最新消息", rootNodeVO, 1);
            NodeVO nodeVO2 = CreateNode("公司介紹", rootNodeVO, 2);
            CreatePost("公司沿革", nodeVO2, 1);
            CreatePost("使命宗旨", nodeVO2, 2);
            CreatePost("關係企業", nodeVO2, 3);
            CreatePost("合作夥伴", nodeVO2, 4);
            CreatePost("交通資訊", nodeVO2, 5);
            CreatePost("公益捐款", nodeVO2, 6);
            NodeVO nodeVO3 = CreateNode("師資陣容", rootNodeVO, 3);
            CreatePost("內部講師", nodeVO3, 1);
            CreatePost("外部講師", nodeVO3, 2);

            NodeVO nodeVO4 = CreateNode("講座活動", rootNodeVO, 4);
            NodeVO nodeVO401 = CreateNode("近期活動", nodeVO4, 1);
            CreateNode("台北場", nodeVO401, 1);
            CreateNode("新竹場", nodeVO401, 2);
            CreateNode("台中場", nodeVO401, 3);
            CreateNode("高雄場", nodeVO401, 4);
            NodeVO nodeVO402 = CreateNode("講座介紹", nodeVO4, 2);

            CreateNode("精彩回顧", rootNodeVO, 5);
            CreateNode("成功見證", rootNodeVO, 6);
            CreateNode("財商文章", rootNodeVO, 7);
            NodeVO nodeVO8 = CreateNode("行事曆", rootNodeVO, 8);
            CreatePost("行事曆", nodeVO8, 1);
            CreateNode("線上諮詢", rootNodeVO, 9);
            NodeVO nodeVO10 = CreateNode("聯絡我們", rootNodeVO, 10);
            CreatePost("聯絡我們", nodeVO10, 1);

            NodeVO nodeVO11 = CreateNode("學員服務條款", rootNodeVO, 11);
            CreatePost("學員服務條款", nodeVO11, 1);
        }

        private PostVO CreatePost(string title, NodeVO nodeVO, int sort)
        {
            PostVO postVO = new PostVO();
            postVO.Node = nodeVO;
            postVO.Title = title;
            postVO.SortNo = sort;
            postVO.Flag = 1;
            postVO.CreatedBy = "admin";
            postVO.UpdatedBy = "admin";
            postVO.CreatedDate = DateTime.Now;
            postVO.UpdatedDate = DateTime.Now;
            postVO.ShowDate = DateTime.Today;

            return m_PostService.CreatePost(postVO);
        }

        private PostVO CreatePost(string title, NodeVO nodeVO, int sort, string linkUrl, string picFileName)
        {
            PostVO postVO = new PostVO();
            postVO.Node = nodeVO;
            postVO.Title = title;
            postVO.SortNo = sort;
            postVO.Flag = 1;
            postVO.CreatedBy = "admin";
            postVO.UpdatedBy = "admin";
            postVO.CreatedDate = DateTime.Now;
            postVO.UpdatedDate = DateTime.Now;
            postVO.ShowDate = DateTime.Today;
            postVO.LinkUrl = linkUrl;
            postVO.PicFileName = picFileName;

            return m_PostService.CreatePost(postVO);
        }

        private NodeVO CreateNode(string name, NodeVO parentNode, int sort)
        {
            NodeVO rootNodeVO = new NodeVO();
            rootNodeVO.Name = name;
            rootNodeVO.ParentNode = parentNode;
            rootNodeVO.SortNo = sort;
            rootNodeVO.Flag = 1;
            rootNodeVO.CreatedBy = "admin";
            rootNodeVO.UpdatedBy = "admin";
            rootNodeVO.CreatedDate = DateTime.Now;
            rootNodeVO.UpdatedDate = DateTime.Now;

            return m_PostService.CreateNode(rootNodeVO);
        }

        private NodeVO CreateNode(string name, NodeVO parentNode, int sort, NodeVO.UnitType unitType, string filePath)
        {
            NodeVO rootNodeVO = new NodeVO();
            rootNodeVO.Name = name;
            rootNodeVO.ParentNode = parentNode;
            rootNodeVO.SortNo = sort;
            rootNodeVO.Flag = 1;
            rootNodeVO.CreatedBy = "admin";
            rootNodeVO.UpdatedBy = "admin";
            rootNodeVO.CreatedDate = DateTime.Now;
            rootNodeVO.UpdatedDate = DateTime.Now;
            rootNodeVO.UType = unitType;
            rootNodeVO.PicFileName = filePath;

            return m_PostService.CreateNode(rootNodeVO);
        }

        private MenuFuncVO CreateParentMenu(string menuName, int sort)
        {
            MenuFuncVO menuFuncVO = new MenuFuncVO(menuName, null);
            menuFuncVO.ListOrder = sort;

            return m_AuthService.CreateMenuFunc(menuFuncVO);
        }

        private MenuFuncVO CreateSubMenu(string menuName, MenuFuncVO parentMenu, int sort, string path)
        {
            MenuFuncVO menuFuncVO = new MenuFuncVO(menuName, parentMenu);
            menuFuncVO.ListOrder = sort;
            menuFuncVO.MainPath = path;

            return m_AuthService.CreateMenuFunc(menuFuncVO);
        }

        private void InitSystemParam()
        {
            //系統設定
            SystemParamVO systemParamVO = new SystemParamVO();
            //systemParamVO.MailSmtp = "smtp.gmail.com";
            //systemParamVO.Account = "test@pro2e.com.tw";
            //systemParamVO.SendEmail = "test@pro2e.com.tw";
            //systemParamVO.MailPort = "587";
            //systemParamVO.EnableSSL = true;
            //systemParamVO.Password = "28005786";
            systemParamVO.MailSmtp = "60.248.19.168";
            systemParamVO.Account = "SmtpUser";
            systemParamVO.SendEmail = "test@test.com";
            systemParamVO.MailPort = "25";
            systemParamVO.EnableSSL = false;
            systemParamVO.Password = "abc+1234";

            m_SystemService.CreateSystemParam(systemParamVO);
        }
    }
}
